package by.yakusekmaxim.bookgid.dataaccess.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.SingularAttribute;

import org.hibernate.jpa.criteria.OrderImpl;
import org.springframework.stereotype.Repository;

import by.yakusekmaxim.bookgid.dataaccess.UserAccountDao;
import by.yakusekmaxim.bookgid.dataaccess.util.UserAccountFilter;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;

@Repository
public class UserAccountDaoImpl extends AbstractDaoImpl<Long, UserAccount>
		implements UserAccountDao {

	protected UserAccountDaoImpl() {
		super(UserAccount.class);
	}

	public Long getCount() {
		CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

		CriteriaQuery<Long> criteria = cBuilder.createQuery(Long.class);
		Root<UserAccount> root = criteria.from(UserAccount.class);
		criteria.select(cBuilder.count(root));

		TypedQuery<Long> query = getEm().createQuery(criteria);
		return query.getSingleResult();
	}

	public Long getCount(final SingularAttribute<UserAccount, ?> attribute,
			Object value) {
		CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

		CriteriaQuery<Long> criteria = cBuilder.createQuery(Long.class);
		Root<UserAccount> root = criteria.from(UserAccount.class);
		criteria.where(cBuilder.equal(root.get(attribute), value));
		criteria.select(cBuilder.count(root));

		TypedQuery<Long> query = getEm().createQuery(criteria);
		return query.getSingleResult();
	}

	@Override
	public List<UserAccount> getAllUserAccount(
			SingularAttribute<UserAccount, ?> attr, boolean ascending,
			int startRecord, int pageSize) {
		CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

		CriteriaQuery<UserAccount> criteria = cBuilder
				.createQuery(UserAccount.class);
		Root<UserAccount> root = criteria.from(UserAccount.class);

		criteria.select(root);
		criteria.orderBy(new OrderImpl(root.get(attr), ascending));

		TypedQuery<UserAccount> query = getEm().createQuery(criteria);
		query.setFirstResult(startRecord);
		query.setMaxResults(pageSize);

		List<UserAccount> results = query.getResultList();
		return results;
	}

	@Override
	public List<UserAccount> getAllUserAccount(
			SingularAttribute<UserAccount, ?> attr, boolean ascending,
			int startRecord, int pageSize, UserAccountFilter filter) {
		
		SingularAttribute<UserAccount, ?> attributeF = filter.getAttribute();
		Object valueF = filter.getValue();
		
		CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

		CriteriaQuery<UserAccount> criteria = cBuilder
				.createQuery(UserAccount.class);
		Root<UserAccount> root = criteria.from(UserAccount.class);
		criteria.where(cBuilder.equal(root.get(attributeF), valueF));
		criteria.select(root);
		criteria.orderBy(new OrderImpl(root.get(attr), ascending));

		TypedQuery<UserAccount> query = getEm().createQuery(criteria);
		query.setFirstResult(startRecord);
		query.setMaxResults(pageSize);

		List<UserAccount> results = query.getResultList();
		return results;
	}

}
